package com.ruizhong.centercontroll.util;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.ruizhong.centercontroll.MainActivity;

public class ZoomView extends RelativeLayout implements ScaleGestureDetector.OnScaleGestureListener {

    public void stopController() {
//        controllThread.stopController();
    }

    private enum Mode {
        NONE,
        DRAG,
        ZOOM
    }

    private static ControllThread controllThread;

    private static final String TAG = "ZoomLayout";
    private static final float MIN_ZOOM = 1.0f;
    private static final float MAX_ZOOM = 4.0f;

    public static Mode mode = Mode.NONE;
    public static float scale = 1.0f;
    private float lastScaleFactor = 0f;


    private float startX = 0f;
    private float startY = 0f;


    private float dx = 0f;
    private float dy = 0f;
    private float prevDx = 0f;
    private float prevDy = 0f;

    public int screenWidth;

    public int screenHeight;

    private String aimIp;

    public ZoomView(
            Context context,
            MainActivity mainActivity,
            int screenWidth,
            int screenHeight,
            String streamUrl) {

        super(context);
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
        aimIp = streamUrl.split("/")[2].split(":")[0];
        init(context,mainActivity);
    }

    public ZoomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,null);
    }

    public ZoomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
//        init(context,null);
    }



    public void init(Context context, MainActivity mainActivity) {
//        final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context, this);
//        controllThread = new ControllThread(aimIp,mainActivity);
//        new Thread(controllThread).start();
//        this.setOnTouchListener(new OnTouchListener() {
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//                // 创建一个整型数组来存储位置信息
//                int[] location = new int[2];
//
//// 调用getLocationOnScreen方法来获取位置信息
//                MainActivity.textureView.getLocationInWindow(location);
//                float x = motionEvent.getX();
//                float y = motionEvent.getY();
//                int textureViewWidth = MainActivity.textureView.getWidth();
//                int textureViewHeight = MainActivity.textureView.getHeight();
//                switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
//                    case MotionEvent.ACTION_DOWN:
//                        if (MainActivity.swipping == false) {
//                            MainActivity.pressStartTime = System.currentTimeMillis();
//
//                           controllThread.sendEvent(new KeyInfo("mouse", 501, x, y, textureViewWidth, textureViewHeight,location[0],location[1],scale,screenWidth,screenHeight));
//                        } else {
//                            if (scale > MIN_ZOOM) {
//                                mode = Mode.DRAG;
//                                startX = x - prevDx;
//                                startY = y - prevDy;
//                            }
//                        }
//                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        if (MainActivity.swipping == false) {
//                            controllThread.sendEvent(new KeyInfo("mouse", 503, x, y, textureViewWidth, textureViewHeight,location[0],location[1],scale,screenWidth,screenHeight));
//                        } else {
//                            if (mode == Mode.DRAG) {
//                                dx = x - startX;
//                                dy = y - startY;
//                            }
//                        }
//                        break;
//                    case MotionEvent.ACTION_POINTER_DOWN:
//                        mode = Mode.ZOOM;
//                        break;
//                    case MotionEvent.ACTION_POINTER_UP:
//                        mode = Mode.DRAG;
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        Log.i(TAG, "UP");
//
//                        if (MainActivity.swipping == false) {
//                            long pressDuration = System.currentTimeMillis() - MainActivity.pressStartTime;
//                            if (pressDuration > 500) { // 假设长按阈值为500毫秒
//                                // 处理长按事件
//                                controllThread.sendEvent(new KeyInfo("mouse", 1007, x, y, textureViewWidth, textureViewHeight,location[0],location[1],scale,screenWidth,screenHeight));
//
//                            } else {
//                                // 处理点击事件
//                                controllThread.sendEvent(new KeyInfo("mouse", 502, x, y, textureViewWidth, textureViewHeight,location[0],location[1],scale,screenWidth,screenHeight));
//                            }
//                        } else {
//                            mode = Mode.NONE;
//                            prevDx = dx;
//                            prevDy = dy;
//                        }
//                        break;
//                }
//                if (MainActivity.swipping == true) {
////                    scaleDetector.onTouchEvent(motionEvent);
//                    if ((mode == Mode.DRAG && scale >= MIN_ZOOM) || mode == Mode.ZOOM) {
//                        getParent().requestDisallowInterceptTouchEvent(true);
//                        float maxDx = (child().getWidth() - (child().getWidth() / scale)) / 2 * scale;
//                        float maxDy = (child().getHeight() - (child().getHeight() / scale)) / 2 * scale;
//                        dx = Math.min(Math.max(dx, -maxDx), maxDx);
//                        dy = Math.min(Math.max(dy, -maxDy), maxDy);
//                        Log.i(TAG, "Width: " + child().getWidth() + ", scale " + scale + ", dx " + dx
//                                + ", max " + maxDx);
//                        applyScaleAndTranslation();
//                    }
//                }
//
//
//                return true;
//            }
//        });
    }


    private void applyScaleAndTranslation() {
        child().setScaleX(scale);
        child().setScaleY(scale);
        child().setTranslationX(dx);
        child().setTranslationY(dy);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return MainActivity.scalenable;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector scaleDetector) {
        Log.i(TAG, "onScaleBegin");
        return true;
    }

    @Override
    public boolean onScale(ScaleGestureDetector scaleDetector) {
        float scaleFactor = scaleDetector.getScaleFactor();
        Log.i(TAG, "onScale" + scaleFactor);
        if (lastScaleFactor == 0 || (Math.signum(scaleFactor) == Math.signum(lastScaleFactor))) {
            scale *= scaleFactor;
            scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
            lastScaleFactor = scaleFactor;
        } else {
            lastScaleFactor = 0;
        }
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector scaleDetector) {
        Log.i(TAG, "onScaleEnd");
    }


    private View child() {
        return getChildAt(0);
    }
}